package com.sh.data.engine.infrastructure.config;

import lombok.Data;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/19 10:11
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "datax")
public class DataXConfig {

    private String home = "/data/datax/datax";

    private String options = "-server -Xms256m -Xmx512m";

    /**
     * 通道数量
     */
    private String gpPath = "/gp_file";

    /**
     * 通道数量
     */
    private Integer channelNum = 4;

    /**
     * 一次性批量提交的记录数大小
     */
    private Integer batchSize = 5000;

    /**
     * 插件和数据库服务器端每次批量数据获取条数
     */
    private Integer fetchSize = 300;

    private Integer allChannelSize = 250000;

    private Integer channelSize = 50000;

    public String getCmd(String jsonPath) {
        StringBuilder sb = new StringBuilder();

        sb.append("java ")
            .append(options)
            .append(" -Ddatax.home=")
            .append(home)
            .append(" -Dfile.encoding=UTF-8 ")
            .append(" -Dlogback.configurationFile=")
            .append(getLogConfigFile())
            .append(" -classpath ")
            .append(getClasspath())
            .append(" com.alibaba.datax.core.Engine")
            .append(" -mode standalone -jobid -1 -job ")
            .append(jsonPath);
        return sb.toString();
    }

    private String getLogConfigFile() {
        return normalizeFile(
            home + IOUtils.DIR_SEPARATOR + "conf" + IOUtils.DIR_SEPARATOR + "logback.xml");
    }

    private String getClasspath() {
        return normalizeFile(getHome() + IOUtils.DIR_SEPARATOR + "lib" + IOUtils.DIR_SEPARATOR + "*");
    }

    private String normalizeFile(String file) {
        if (StringUtils.isBlank(file)) {
            return null;
        }
        return new File(file).getPath();
    }
}
